package com.zbdx.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zbdx.bean.College;
import com.zbdx.dao.SchoolDao;
import com.zbdx.dao.impl.SchoolDaoImpl;

/**
 * Servlet implementation class CollegeServlet
 */
@WebServlet("/CollegeServlet")
public class CollegeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CollegeServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String method=request.getParameter("method");
		System.out.println(method);
		if("find".equals(method)) {//当前选择项==findAll
			find(request, response);
		}else if("add".equals(method)) {//当前选择项==add
			add(request, response);
		}else if("update".equals(method)) {
			update(request, response);
		}else if("delete".equals(method)){
			delete(request, response);
		}else if("updates".equals(method)){
			updates(request, response);
		}else {
			System.out.println("");
		}
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void find(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		SchoolDao dao = new SchoolDaoImpl();
		List<College> colleges = dao.findcollege();
		//System.out.println(colleges);
		request.setAttribute("college", colleges);
		request.getRequestDispatcher("/college_list.jsp").forward(request, response);
		
	}

	protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String collegeName=request.getParameter("collegeName");
		String collegeRoom=request.getParameter("collegeRoom");
		String collegeTel=request.getParameter("collegeTel");
		String collegeEmail=request.getParameter("collegeEmail");
		int collegeDeanID=Integer.parseInt(request.getParameter("collegeDeanID"));
		//System.out.println(collegeName);
		College college = new College(collegeName, collegeRoom, collegeDeanID, collegeTel, collegeEmail);
		//System.out.println(college);
		SchoolDao dao = new SchoolDaoImpl();
		boolean re=dao.addCollege(college);
		if(re) {
			response.sendRedirect(request.getContextPath()+"/CollegeServlet?method=find");
			
		}else {
			response.sendRedirect(request.getContextPath()+"/error.html");
		}
	}
	
	protected void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//String collegeId = request.getParameter("id");
		String s_id = request.getParameter("id");
		int id=0;
		if("".equals(s_id)||s_id==null) {
			System.out.println(s_id+"没有值");
			return;
		}else {
			id=Integer.parseInt(s_id);
		}
//		System.out.println(id);
		int collegeId = Integer.parseInt(request.getParameter("id"));
		String collegeName = request.getParameter("collegeName");
		String collegeRoom = request.getParameter("collegeRoom");
		int collegeDeanID=Integer.parseInt(request.getParameter("collegeDeanID"));
		String collegeTel=request.getParameter("collegeTel");
		String collegeEmail = request.getParameter("collegeEmail");
		
		College college = new College(collegeId, collegeName, collegeRoom, collegeDeanID, collegeTel, collegeEmail);
		//System.out.println(manager);
		System.out.println(college);
		//dao--add(manager)---jdbc
		SchoolDao dao=new SchoolDaoImpl();
		boolean re=dao.updateCollege(college);
		if(re) {
			response.sendRedirect(request.getContextPath()+"/CollegeServlet?method=find");
		}else {
			response.sendRedirect(request.getContextPath()+"/error.html");
		}
	}
	
	
	protected void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int id = Integer.parseInt(request.getParameter("id"));
//		System.out.println(id);		
		SchoolDao dao = new SchoolDaoImpl();		
		boolean re = dao.deleteCollege(id);
		if(re) {
			System.out.println("删除成功！");
			response.sendRedirect(request.getContextPath()+"/CollegeServlet?method=find");
		}else {
			System.out.println("删除失败！");
			response.sendRedirect(request.getContextPath()+"/error.html");
		}
		
	}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	
	protected void updates(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String s_id = request.getParameter("id");
		int id=0;
		if("".equals(s_id)||s_id==null) {
			System.out.println(s_id+"没有值");
			return;
		}else {
			id=Integer.parseInt(s_id);
		}
		SchoolDao dao=new SchoolDaoImpl();
		College college =  dao.findColl(id);
		request.setAttribute("m", college);
		request.getRequestDispatcher("college_update.jsp").forward(request, response);
	
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		doGet(request, response);
	}

}
